package com.travel.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.travel.dto.UserInfoDTO;
import com.travel.pojo.ScenicSpot;
import com.travel.pojo.User;
import com.travel.result.Result;
import com.travel.service.ScenicSpotService;
import com.travel.service.UserOrderService;
import com.travel.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: travel
 * @author: lvyaxin
 * @create: 2024-07-08 15:56
 **/
@RestController
@RequestMapping("/api/common")
@Api(tags = "首页旅游项目查询")
public class ComController {

    @Autowired
    private ScenicSpotService scenicSpotService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserOrderService userOrderService;

    // 查询全部旅游项目
    @GetMapping("/listAll")
    @ApiOperation("查询全部旅游项目")
    public Result<List<ScenicSpot>> listScenic() {
        LambdaQueryWrapper<ScenicSpot> wrapper = new LambdaQueryWrapper<ScenicSpot>()
                .eq(ScenicSpot::getDeleteStatus, 0)
                .eq(ScenicSpot::getState, 1);
        List<ScenicSpot> list = scenicSpotService.list(wrapper);
        return Result.ok(list);
    }

    // 查询旅游项目所属线路
    /*@GetMapping("/listRoutesByTourId")
    @ApiOperation("查询旅游项目所属线路")
    public Result<List<TravelRoute>> listRoutesByTourId(
            @ApiParam(value = "旅游项目id", required = true)
            @RequestParam Long id,
            @ApiParam(value = "线路状态（0：包含，1：自费）,不传参：查全部", required = false)
            @RequestParam(required = false) Long state) {
        LambdaQueryWrapper<TravelRoute> wrapper = new LambdaQueryWrapper<TravelRoute>()
                .eq(TravelRoute::getAddUserId, id)
                .eq(state!=null, TravelRoute::getState, state);
        List<TravelRoute> list = travelRouteService.list(wrapper);
        return Result.ok(list);
    }*/

    // 根据id查询旅游项目
    @GetMapping("/listScenicById/{id}")
    @ApiOperation("根据id查询旅游项目")
    public Result<List<ScenicSpot>> listScenicById(
            @ApiParam(value = "旅游项目id", required = true)
            @PathVariable String id) {
        LambdaQueryWrapper<ScenicSpot> wrapper = new LambdaQueryWrapper<ScenicSpot>()
                .eq(ScenicSpot::getId, id)
                .eq(ScenicSpot::getDeleteStatus, 0);
        List<ScenicSpot> list = scenicSpotService.list(wrapper);
        return Result.ok(list);
    }

    // 获取banner
    @GetMapping("/banner")
    @ApiOperation("获取首页banner图")
    public Result<List<String>> listBanner() {

        LambdaQueryWrapper<ScenicSpot> wrapper = new LambdaQueryWrapper<ScenicSpot>()
                .eq(ScenicSpot::getDeleteStatus, 0);
        List<ScenicSpot> list = scenicSpotService.list(wrapper);

        // 提取所有url并封装到List<String>中
        List<String> banners = list.stream()
                .map(ScenicSpot::getImgUrl)
                .collect(Collectors.toList());

        return Result.ok(banners);
    }

    // 获取用户信息(登录或注册)
    @PostMapping("/getUserInfo")
    @ApiOperation("根据手机号获取用户信息(登录或注册)")
    public Result<User> getUserInfoByPhone(@RequestParam String phone) {
        User user = userService.loginOrRegister(phone);
        return Result.ok(user);
    }

    // 07-07 根据订单id修改合同
    @PutMapping("/update/{orderId}")
    @ApiOperation("根据订单id修改订单")
    public Result updateContract(@RequestBody UserInfoDTO userInfoDTO, @PathVariable String orderId){
        userOrderService.updateUserInfo(userInfoDTO, orderId);
        return Result.ok();
    }

}
